/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-02 10:56:50
 * @LastEditTime: 2019-09-02 11:06:33
 * @LastEditors: Please set LastEditors
 */
/*
 * @lc app=leetcode.cn id=23 lang=javas cript
 *
 * [23] 合并K个排序链表
 *
 * https://leetcode-cn.com/problems/merge-k-sorted-lists/description/
 *
 * algorithms
 * Hard (47.42%)
 * Likes:    322
 * Dislikes: 0
 * Total Accepted:    39.4K
 * Total Submissions: 83.2K
 * Testcase Example:  '[[1,4,5],[1,3,4],[2,6]]'
 *
 * 合并 k 个排序链表，返回合并后的排序链表。请分析和描述算法的复杂度。
 * 
 * 示例:
 * 
 * 输入:
 * [
 * 1->4->5,
 * 1->3->4,
 * 2->6
 * ]
 * 输出: 1->1->2->3->4->4->5->6
 * 
 */
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode[]} lists
 * @return {ListNode}
 */
var mergeKLists = function(lists) {
    let nums = []
    const ans = []
    for(let i=0;i<lists.length;i++){
        let list = lists[i]
        while(list){
            nums.push(list.val)
            list = list.next
        }
    }
    if(nums.length == 0){
        return null
    }else{
        nums = nums.sort((a,b)=>a-b)
        let a = ans
        nums.forEach(item=>{
            a.next = {val:item}
            a = a.next
        })
        return ans.next
    }
    


};

